import 'package:flutter/material.dart';
import 'package:get/get_core/get_core.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
import 'package:get_storage/get_storage.dart';
import 'package:get/get.dart';

///存储操作类
class StoreService extends GetxService {
  final GetStorage _box = GetStorage();

  Future<StoreService> init() async {
    await GetStorage.init();
    return this;
  }

  static StoreService get to => Get.find();

  ///保存数据
  void put(String key, value) {
    _box.write(key, value);
  }

  ///移除数据
  void remove(key) {
    _box.remove(key);
  }

  ///获取数据
  getValue<T>(key) {
    return _box.read(key);
  }

  getStringValue(key) {
    return getValue<String>(key);
  }

  ///监听
  void listenKey(String key, ValueSetter callback) {
    _box.listenKey(key, (value) {
      callback.call(value);
    });
  }

  void removeAll() {
    _box.erase();
  }
}
